unit newecformunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, ExtCtrls,
  Buttons, StdCtrls, database;

const
  EC_SELECTION_CANCELLED = 0;

type

  { TNewECForm }

  TNewECForm = class(TForm)
    ActorGroupBox: TGroupBox;
    AudioAndVideoGroupBox: TGroupBox;
    BreakLoopBitBtn: TBitBtn;
    TintPictureBitBtn: TBitBtn;
    ShowPictureBitBtn: TBitBtn;
    MovePictureBitBtn: TBitBtn;
    ErasePictureBitBtn: TBitBtn;
    RotatePictureBitBtn: TBitBtn;
    PictureAndWeatherGroupBox: TGroupBox;
    SetWeatherEffectBitBtn: TBitBtn;
    TintScreenBitBtn: TBitBtn;
    CallCommonEventBitBtn: TBitBtn;
    ShakeScreenBitBtn: TBitBtn;
    ChangeArmoursBitBtn: TBitBtn;
    ChangeClassBitBtn: TBitBtn;
    ChangeEquipmentBitBtn: TBitBtn;
    PlayMovieBitBtn: TBitBtn;
    ChangeExpBitBtn: TBitBtn;
    FadeoutBgSoundsBitBtn: TBitBtn;
    ChangeGoldBitBtn: TBitBtn;
    WaitBitBtn: TBitBtn;
    ChangeHpBitBtn: TBitBtn;
    PlayBgMusicBitBtn: TBitBtn;
    ChangeItemsBitBtn: TBitBtn;
    ChangeLevelBitBtn: TBitBtn;
    PlayMusicEffectBitBtn: TBitBtn;
    ChangeMpBitBtn: TBitBtn;
    FadeoutBgMusicBitBtn: TBitBtn;
    ChangeNameBitBtn: TBitBtn;
    ChangeNicknameBitBtn: TBitBtn;
    ChangeParamBitBtn: TBitBtn;
    PlaySoundEffectBitBtn: TBitBtn;
    ChangePartyMembersBitBtn: TBitBtn;
    ChangeProfileBitBtn: TBitBtn;
    ChangeSkillBitBtn: TBitBtn;
    StopSoundEffectsBitBtn: TBitBtn;
    ChangeStateBitBtn: TBitBtn;
    RestoreSavedMusicBitBtn: TBitBtn;
    ChangeTpBitBtn: TBitBtn;
    SaveBgMusicBitBtn: TBitBtn;
    ChangeWeaponsBitBtn: TBitBtn;
    CommentBitBtn: TBitBtn;
    ConditionalBranchBitBtn: TBitBtn;
    FadeoutScreenBitBtn: TBitBtn;
    ControlTimerBitBtn: TBitBtn;
    ShowAnimationBitBtn: TBitBtn;
    EcPage1Panel: TPanel;
    EcPage2Panel: TPanel;
    EcPage2ScrollBox: TScrollBox;
    EndEventProcessingBitBtn: TBitBtn;
    FlashScreenBitBtn: TBitBtn;
    FlowControlGroupBox: TGroupBox;
    FlowControlGroupBox1: TGroupBox;
    FullRecoveryBitBtn: TBitBtn;
    PlayBgSoundBitBtn: TBitBtn;
    GameDataGroupBox: TGroupBox;
    GameDataGroupBox1: TGroupBox;
    GotoBitBtn: TBitBtn;
    InputNumberBitBtn: TBitBtn;
    SetEventLocationBitBtn: TBitBtn;
    LabelBitBtn: TBitBtn;
    LoopBitBtn: TBitBtn;
    FadeinScreenBitBtn: TBitBtn;
    MessageGroupBox: TGroupBox;
    MovementGroupBox: TGroupBox;
    Page1Col1Panel: TPanel;
    Page2Col1Panel: TPanel;
    Page1Col2Panel: TPanel;
    Page1Col2Panel1: TPanel;
    PartyGroupBox: TGroupBox;
    EcPage1ScrollBox: TScrollBox;
    WaitGroupBox: TGroupBox;
    ScrollTextBitBtn: TBitBtn;
    SetMovementRouteBitBtn: TBitBtn;
    SelectItemBitBtn: TBitBtn;
    ScrollMapBitBtn: TBitBtn;
    GetOnOffVehicleBitBtn: TBitBtn;
    SetSelfSwitchBitBtn: TBitBtn;
    GatherFollowersBitBtn: TBitBtn;
    SetSwitchesBitBtn: TBitBtn;
    ChangePlayerVisibilityBitBtn: TBitBtn;
    SetVariablesBitBtn: TBitBtn;
    ChangeFollowerVisibilityBitBtn: TBitBtn;
    ShowBalloonIconBitBtn: TBitBtn;
    EraseCurrentEventIconBitBtn: TBitBtn;
    ShowChoiceBitBtn: TBitBtn;
    CancelBitBtn: TBitBtn;
    ECPageControl: TPageControl;
    ECPage1TabSheet: TTabSheet;
    ECPage2TabSheet: TTabSheet;
    ECPage3TabSheet: TTabSheet;
    CancelPanel: TPanel;
    SetVehicleLocationBitBtn: TBitBtn;
    ShowMessageBitBtn: TBitBtn;
    TransferPlayerBitBtn: TBitBtn;
    procedure CommandBitBtnClick(Sender: TObject);
    procedure ECPageControlChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    procedure UpdateCaptions;
    procedure UpdateChangeGoldName;
    procedure UpdateChangePointsName(Button: TBitBtn; FormatString, DefaultPoints: String; PointType: Integer);

    procedure ResizeButtons;
  public
    SelectedCode: Integer;
    Db: TDatabase;

    procedure SetDatabase(ADb: TDatabase);
    function ShowSelection: Integer;
    function ShowModal: Integer; override;
  end;

var
  NewECForm: TNewECForm;

resourcestring
  rsChangeGoldEc = 'Change %s (currency)...';
  rsChangeHpEc = 'Change %s (hit points)...';
  rsChangeMpEc = 'Change %s (magic points)...';
  rsChangeTpEc = 'Change %s (technique points)...';

  rsHp = 'HP';
  rsMp = 'MP';
  rsTp = 'TP';


implementation

{$R *.lfm}

uses
  constants;

{ TNewECForm }

procedure TNewECForm.CommandBitBtnClick(Sender: TObject);
begin
  SelectedCode := (Sender as TComponent).Tag;
  ModalResult := mrOK
end;

procedure TNewECForm.ECPageControlChange(Sender: TObject);
begin

end;

procedure TNewECForm.FormCreate(Sender: TObject);
begin
  UpdateCaptions;
end;

procedure TNewECForm.FormResize(Sender: TObject);
begin
  ResizeButtons
end;

procedure TNewECForm.UpdateCaptions;
begin
  UpdateChangeGoldName;
  UpdateChangePointsName(ChangeHpBitBtn, rsChangeHpEc, rsHp, 1);
  UpdateChangePointsName(ChangeMpBitBtn, rsChangeMpEc, rsMp, 2);
  UpdateChangePointsName(ChangeTpBitBtn, rsChangeTpEc, rsTp, 3);
end;

procedure TNewECForm.UpdateChangeGoldName;
var
  Currency: String = '';
begin
  if Db <> nil then
    Currency := Db.System.Strings['currencyUnit'];
  if Trim(Currency) = '' then
    Currency := rsCurrency;

  ChangeGoldBitBtn.Caption := rsChangeGoldEc.Format([Currency]);
end;

procedure TNewECForm.UpdateChangePointsName(Button: TBitBtn;
  FormatString, DefaultPoints: String; PointType: Integer);
var
  PointNameIndex: Integer;
  PointName: String;
begin
  PointNameIndex := PointType * 2;

  if Db <> nil then
    PointName := Db.System.Objects['terms'].Arrays['basic'].Strings[PointNameIndex];

  if Trim(PointName) = '' then
    PointName := DefaultPoints;

  Button.Caption := FormatString.Format([PointName]);
end;

procedure TNewECForm.ResizeButtons;
  procedure ResizeSingleColumn(Column: TPanel; ScrollBox: TScrollBox);
  var
    NewWidth: Integer;
    I: Integer;
    GroupBox: TComponent;
  begin
    NewWidth := (ScrollBox.Width - Scale96ToForm(8) - ScrollBox.HorzScrollBar.Size) div 2;
    Column.Width := NewWidth;

    for I := 0 to Column.ComponentCount -1 do begin
      GroupBox := Column.Components[I];
      if GroupBox is TGroupBox then
        (GroupBox as TGroupBox).Width := NewWidth;
    end;
  end;
begin
  ResizeSingleColumn(Page1Col1Panel, EcPage1ScrollBox);
  ResizeSingleColumn(Page1Col2Panel, EcPage1ScrollBox);
end;

procedure TNewECForm.SetDatabase(ADb: TDatabase);
begin
  Db := ADb;
  UpdateCaptions
end;

function TNewECForm.ShowSelection: Integer;
begin
  ShowModal;
  if ModalResult = mrOK then
    ShowSelection := SelectedCode
  else
    ShowSelection := EC_SELECTION_CANCELLED;
end;

function TNewECForm.ShowModal: Integer;
begin
  ResizeButtons;
  ShowModal := inherited ShowModal;
end;

end.

